namespace ghost {
    export class BarrierUI extends Laya.Box implements IListener
    {
        public barrierSp:Laya.Sprite;		
		public _sceneMgr:SceneManager;
		private _mapRedrawId:number;	
		private _txtVec:Laya.Text[];
		public settingBtn:Laya.Button;
		public touchCtrl:ToucherCtrl;
		/**是否允许障碍编辑*/public enabledBarrierSet:boolean;
		
		public barrierSettingUI:BarrierSettingUI;
		private _insertBarriers:any;

		public constructor(sceneMgr:SceneManager) {
			super();
			let s = this;
			s.left = s.right = s.top = s.bottom = 0;
			
			s._sceneMgr = sceneMgr;
			s.mouseThrough = true;			
			s._mapRedrawId = s._sceneMgr.mapRedrawListener.addListener(s);			
			s.touchCtrl = new ToucherCtrl();					
			
			s._txtVec = [];			
			s.enabledBarrierSet = true;
			

			Laya.loader.load(Conf.uiAlias + "barrier.atlas", Laya.Handler.create(this, this.atlasLoadComp));			
		}
		private atlasLoadComp():void
		{
			let s= this;
			Laya.loader.load(Conf.uiAlias + "barrierUI.atlas", Laya.Handler.create(this, ()=>{
				BarrierUILayout.getIntance().layout(s);					
				s._sceneMgr.sceneLayer.addChild(s.barrierSp);									
				
				s.barrierSettingUI.show(s);
				let editorData:any;
				editorData = s._sceneMgr.sceneCfg.editorData;
				s._insertBarriers = editorData && editorData.insertBarries?editorData.insertBarries:{};
				s.settingBtn.on(Laya.Event.CLICK, s, s.oper);
				s.reDrawBarrier();
				s.resize();
			}));
			
		}
		
		public sceneCfgChange:()=>void = ():void=>{
			let s = this;
			s._sceneMgr.resetBarrier();
		}
		
		private oper(e:Laya.Event):void
		{
			let s= this;
			if(e.currentTarget == s.settingBtn)
			{
				if(s.barrierSettingUI.parent == null)
					s.barrierSettingUI.show(s);
				else
					s.barrierSettingUI.hide();
			}

		}
		
		public reDrawBarrier():void
		{
			if(!GameManager.debug)return;
			if(this.barrierSp == null)return;
			let i:number,len:number,j:number,len2:number;
			let col:number,row:number,offX:number,offY:number,value:number;
			let gridSize:number,maxRow:number,maxCol:number;
            let color:number;
			let s = this;
			let g:Laya.Graphics;
			let tX:number,tY:number,mX:number,mY:number;
			let stCol:number,stRow:number,mapGridCol:number;
			
			let mainTex:Laya.Texture;
			mainTex = Laya.Loader.getRes("res/atlas/barrier.png");
			
			gridSize = s._sceneMgr.gridSize;
			mX = s._sceneMgr.mapX;
			mY = s._sceneMgr.mapY;
			maxCol = s._sceneMgr.maxAreaCol;
			maxRow = s._sceneMgr.maxAreaRow;
			mapGridCol = s._sceneMgr.mapGridCol;
			offX = mX % gridSize;
			offY = mY % gridSize;
			stCol = mX / gridSize | 0;
			stRow = mY / gridSize | 0;
			len = stRow+Math.ceil(s._sceneMgr.sceneHeight / gridSize) + 1;
			len2 = stCol+Math.ceil(s._sceneMgr.sceneWidth / gridSize) + 1;
			s.barrierSp.graphics.clear();	
			
			let index:number;
			
			// g.clear();					
			// s.clearTxt();
			for(i=stRow;i<len;++i)//行
			{				
				row = row < 0?i + maxRow:i;				
				for(j=stCol;j<len2;++j)//列
				{	
					col = col < 0?j + maxCol:j;								
					index = row*mapGridCol + col;
					// value = s._sceneMgr.sceneCfg.barriers[index];
					value = s._sceneMgr.barrier.getValue(col, row);
					tX = 1 - offX + (j - stCol) * gridSize;
					tY = 1 - offY + (i - stRow) * gridSize;

					
					if(value == null || (value & map2D.Barrier.LAND) > 0 || (value & map2D.Barrier.AIR) > 0)
					{
						if(s._insertBarriers && s._insertBarriers[row] && s._insertBarriers[row][col])
							value = s._insertBarriers[row][col];
					}					
					if(value == null || value == map2D.Barrier.LAND)											
						continue;
					// color = 0xffffff * (value & Barrier.PASS) / Barrier.PASS;			
					let uv:number[];
					let tex:Laya.Texture;
					tex = Laya.Loader.getRes("barrier/" + (value & map2D.Barrier.PASS) + ".png");					
					s.barrierSp.graphics.drawTexture(tex, tX, tY, gridSize, gridSize,null,0.5);
					
					
					if((value & map2D.Barrier.SHADOW) > 0)					   
					{
						tex = Laya.Loader.getRes("barrier/0.png");						
						s.barrierSp.graphics.drawTexture(tex, tX, tY, gridSize, gridSize)
											
					}						
				}
			}			
			
		}
		public dataChange(listenId: number): void
		{let s = this;
			if(listenId == s._mapRedrawId)
			{
				s.reDrawBarrier();
			}
		}
        protected resize(e:Laya.Event=null):void
        {
			let s = this;
			if(s.settingBtn == null)return;
			let bound = Module.getInstance().frameBound;			
			s.settingBtn.x = bound.x + 20;
			s.settingBtn.y = bound.y + 20;
			s.barrierSettingUI.x = bound.x + 20;
        }
		public show(pr:Laya.Sprite=null):void
		{
            let s= this;			
            if(s.parent == null)
                Laya.stage.on(Laya.Event.RESIZE,s,s.resize);
			s._sceneMgr.sceneLayer.mouseEnabled = true;
			s._sceneMgr.sceneLayer.mouseThrough = false;				
            if(pr != s.parent)
            {
                pr.addChild(s);			
            }				
			if(s._mapRedrawId == -1)
				s._mapRedrawId = s._sceneMgr.mapMoveListener.addListener(s);
			s._sceneMgr.gameContainer.mouseThrough = false;
			s.touchCtrl.addTarget(s._sceneMgr.sceneLayer);
			s.touchCtrl.addClass(RoleSkin);
			s.touchCtrl.on(Laya.Event.MOUSE_DOWN, s, s.touchBegin);
			s.touchCtrl.on(Laya.Event.DRAG_MOVE, s, s.touchMove);
			s.touchCtrl.on(Laya.Event.MOUSE_UP, s, s.touchEnd);
			s.touchCtrl.whenDragStopTouch(false);			
			// s._sceneMgr.editable(s.enabledBarrierSet);
			// s.reDrawBarrier();
			s.resize();			
		}
		public hide():void
		{
            let s= this;			
			if(s.parent)
            {
                if((<any>s.parent).removeElement!=null)
                    (<any>s.parent).removeElement(s);
                else
                    (<any>s.parent).removeChild(s);
                Laya.stage.off(Laya.Event.RESIZE,s,s.resize);
            }	
			if(s._mapRedrawId > -1)
			{
				s._sceneMgr.mapMoveListener.removeListener(s);
				s._mapRedrawId = -1;
			}
			s.touchCtrl.removeTarget(s._sceneMgr.sceneLayer);
			s.touchCtrl.removeClass(RoleSkin);
			s.touchCtrl.off(Laya.Event.MOUSE_DOWN, s, s.touchBegin);
			s.touchCtrl.off(Laya.Event.DRAG_MOVE, s, s.touchMove);
			s.touchCtrl.off(Laya.Event.MOUSE_UP, s, s.touchEnd);
			s.touchCtrl.whenDragStopTouch(true);							
		}				
		/**获取障碍值*/
		private getValue():number
		{
			return this.barrierSettingUI.getValue();
		}
		public touchBegin(data:any):void
		{
			let s = this;
			let absX:number,absY:number;
			if(!s.barrierSettingUI)return;
			if(data)
			{
				let sceneLay:Laya.Sprite;
				sceneLay = Module.getInstance().sceneLay;
				absX = s._sceneMgr.mapX + sceneLay.mouseX;
				absY = s._sceneMgr.mapY + sceneLay.mouseY;
				if(s.enabledBarrierSet)
				{					
					s.setBarrier(absX, absY);
				}	
				
				return;
			}		
		}
		public touchMove(data:any):void
		{
			let s = this;
			if(!s.barrierSettingUI)return;
			if(!data)return;
			let absX:number,absY:number;
			absX = s._sceneMgr.mapX + data.currentTargetMouseX;
			absY = s._sceneMgr.mapY + data.currentTargetMouseY;
			if(s.enabledBarrierSet)			
				s.setBarrier(absX, absY);			
		}
		public touchEnd(data:any):void
		{
			let s= this;
			if(!s.barrierSettingUI)return;
			// s._touchBeginX = s._touchBeginY = NaN;
			if(!s.enabledBarrierSet)return;
		}
		private setBarrier(x:number,y:number):void
		{
			let s =this;
			let col:number,row:number;			
			let gridSize:number;			
			// let pt1:Laya.Vector3,pt2:Laya.Vector3,n:Laya.Vector3,pt:Laya.Vector3;
			// pt1 = s._sceneMgr.camera.D2PointToD3(x,y);
			// pt1 = s._sceneMgr.camera.transformInvertD3Point(pt1,pt1);
			// pt2 = s._sceneMgr.camera.D2PointToD3(x,y,null,9999999);
			// pt2 = s._sceneMgr.camera.transformInvertD3Point(pt2,pt2);
			// pt = new Laya.Vector3;
			// n = new Laya.Vector3(0,100,0);
			// Log.writeLog("test",pt1,pt2,pt,n,MathUtil.rayPoint(pt1,pt2,pt,n));
			
			gridSize = s._sceneMgr.gridSize;
			// pt1 = D3Point.pointConvert(0,0,0,s._sceneMgr._enabled3D?s._sceneMgr:null);
			// pt2 = D3Point.pointConvert(s._sceneMgr.mapMaxX,s._sceneMgr.mapMaxY,0,s._sceneMgr.enabled3D?s._sceneMgr:null);
			// col = (s._sceneMgr.mapMaxX * (x - pt1.x)/(pt2.x - pt1.x)) / gridSize | 0;
			// row = (s._sceneMgr.mapMaxY * (y - pt1.y)/(pt2.y - pt1.y)) / gridSize | 0;

			col = x / gridSize | 0;
			row = y / gridSize | 0;
			let flag:number;
			flag = s.getValue();
			s._sceneMgr.sceneCfg.barriers[col + row * s._sceneMgr.mapGridCol] = flag;
			s._sceneMgr.barrier.setBarrierValue(col, row, flag);
			s.reDrawBarrier();
		}
		
		
    }
}